package com.lsyw.intergrate.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lsyw.intergrate.common.Result;
import com.lsyw.intergrate.entity.Area;
import com.lsyw.intergrate.entity.common.PageInfo;
import com.lsyw.intergrate.service.AreaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 行政区划 前端控制器
 * </p>
 *
 * @author 刘磊
 * @since 2024-01-25
 */
@Api(tags = "行政区划")
@RestController
@RequestMapping("/area")
public class AreaController {


    @Autowired
    private AreaService areaService;


    /**
     * 通过id查询
     */
    @GetMapping("/get-by-id")
    public Result getById(String id) {
        return Result.successResult(areaService.getById(id));
    }

    /**
     * 新增
     */
    @PostMapping("/save")
    public Result save(@RequestBody Area area) {
        areaService.save(area);
        return Result.successResult(null);
    }

    /**
     * 通过id删除
     */
    @GetMapping("/delete-by-id/{id}")
    public Result delete(@PathVariable(value = "id") String ids) {
        String[] idsStrs = ids.split(",");
        for (String id : idsStrs) {
            areaService.removeById(Long.parseLong(id));
        }
        return Result.successResult(null);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public Result updateById(@RequestBody Area area) {
        areaService.updateById(area);
        return Result.successResult(null);
    }


    /**
     * 查询列表
     */
    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pId", value = "pId 为0 是省", required = true, paramType = "query", dataType = "String")
    })
    @ApiOperation("根据父类获取下级")
    public Result list(String pId) {
        LambdaQueryWrapper<Area> lambda = new QueryWrapper<Area>().lambda();
        lambda.eq(Area::getParentId, pId);
        return Result.successResult(areaService.list(lambda));
    }

    /**
     * 分页查询
     */
    @PostMapping("/page")
    public Result page(@RequestBody PageInfo pageInfo) {
        Page<Area> page = new Page<>();
        page.setSize(pageInfo.getPageSize());
        page.setCurrent(pageInfo.getPageNum());
        final LambdaQueryWrapper<Area> lambda = new QueryWrapper<Area>().lambda();
        //TODO 自己的查询逻辑
        return Result.successResult(this.areaService.page(page, lambda));
    }


    /**
     * 构造查询条件
     *
     * @param lambda
     * @param param
     */
    private void buildCondition(LambdaQueryWrapper<Area> lambda, AreaReqVo param) {
        if (!StringUtils.isEmpty(param.getId())) {
            lambda.eq(Area::getId, param.getId());
        }
        if (!StringUtils.isEmpty(param.getParentId())) {
            lambda.eq(Area::getParentId, param.getParentId());
        }
        if (!StringUtils.isEmpty(param.getLevel())) {
            lambda.eq(Area::getLevel, param.getLevel());
        }
        if (!StringUtils.isEmpty(param.getName())) {
            lambda.eq(Area::getName, param.getName());
        }
        if (!StringUtils.isEmpty(param.getWholeName())) {
            lambda.eq(Area::getWholeName, param.getWholeName());
        }
        if (!StringUtils.isEmpty(param.getLon())) {
            lambda.eq(Area::getLon, param.getLon());
        }
        if (!StringUtils.isEmpty(param.getLat())) {
            lambda.eq(Area::getLat, param.getLat());
        }
        if (!StringUtils.isEmpty(param.getCityCode())) {
            lambda.eq(Area::getCityCode, param.getCityCode());
        }
        if (!StringUtils.isEmpty(param.getZipCode())) {
            lambda.eq(Area::getZipCode, param.getZipCode());
        }
        if (!StringUtils.isEmpty(param.getAreaCode())) {
            lambda.eq(Area::getAreaCode, param.getAreaCode());
        }
        if (!StringUtils.isEmpty(param.getPinYin())) {
            lambda.eq(Area::getPinYin, param.getPinYin());
        }
        if (!StringUtils.isEmpty(param.getSimplePy())) {
            lambda.eq(Area::getSimplePy, param.getSimplePy());
        }
        if (!StringUtils.isEmpty(param.getPerPinYin())) {
            lambda.eq(Area::getPerPinYin, param.getPerPinYin());
        }
        lambda.orderBy(true, false, Area::getId);
    }


    /**
     * 请求model
     */
    @Data
    @AllArgsConstructor
    private static class AreaReqVo extends Area {
    }


}
